# block structure

Each line that is processed has an effect on this tree. The line is analyzed and, depending on its contents, the document may be altered in one or more of the following ways:

  1. One or more open blocks may be closed.
  2. One or more new blocks may be created as children of the last open block.
  3. Text may be added to the last (deepest) open block remaining on the tree.

Once a line has been incorporated into the tree in this way, it can be discarded, so input can be read in a stream.
For each line, we follow this procedure:

  1. First we iterate through the open blocks, starting with the root document, and descending through last children down to the last open block. Each block imposes a condition that the line must satisfy if the block is to remain open. For example, a block quote requires a > character. A paragraph requires a non-blank line. In this phase we may match all or just some of the open blocks. But we cannot close unmatched blocks yet, because we may have a lazy continuation line (opens new window).
  2. Next, after consuming the continuation markers for existing blocks, we look for new block starts (e.g. >for a block quote). If we encounter a new block start, we close any blocks unmatched in step 1 before creating the new block as a child of the last matched block.
  3. Finally, we look at the remainder of the line (after block markers like >, list markers, and indentation have been consumed). This is text that can be incorporated into the last open block (a paragraph, code block, heading, or raw HTML).

Setext headings are formed when we see a line of a paragraph that is a setext heading underline (opens new window).
Reference link definitions are detected when a paragraph is closed; the accumulated text lines are parsed to see if they begin with one or more reference link definitions. Any remainder becomes a normal paragraph.
We can see how this works by considering how the tree above is generated by four lines of Markdown:

> Lorem ipsum dolor
sit amet.
> - Qui *quodsi iracundia*
> - aliquando id

At the outset, our document model is just

-> document

The first line of our text,

> Lorem ipsum dolor

causes a block_quote block to be created as a child of our open document block, and a paragraphblock as a child of the block_quote. Then the text is added to the last open block, the paragraph:

-> document
  -> block_quote
    -> paragraph
         "Lorem ipsum dolor"

The next line,

sit amet.

is a “lazy continuation” of the open paragraph, so it gets added to the paragraph’s text:

-> document
  -> block_quote
    -> paragraph
         "Lorem ipsum dolor\nsit amet."

The third line,

> - Qui *quodsi iracundia*

causes the paragraph block to be closed, and a new list block opened as a child of the block_quote. A list_item is also added as a child of the list, and a paragraph as a child of the list_item. The text is then added to the new paragraph:

-> document
  -> block_quote
       paragraph
         "Lorem ipsum dolor\nsit amet."
    -> list (type=bullet tight=true bullet_char=-)
      -> list_item
        -> paragraph
             "Qui *quodsi iracundia*"

The fourth line,

> - aliquando id

causes the list_item (and its child the paragraph) to be closed, and a new list_item opened up as child of the list. A paragraph is added as a child of the new list_item, to contain the text. We thus obtain the final tree:

-> document
  -> block_quote
       paragraph
         "Lorem ipsum dolor\nsit amet."
    -> list (type=bullet tight=true bullet_char=-)
         list_item
           paragraph
             "Qui *quodsi iracundia*"
      -> list_item
        -> paragraph
             "aliquando id"